package com.betterjr.modules.temhandler.dubboclient;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.betterjr.common.web.AjaxObject;
import com.betterjr.common.web.ControllerExceptionHandler;
import com.betterjr.common.web.ControllerExceptionHandler.ExceptionHandler;
import com.betterjr.common.web.Servlets;
import com.betterjr.modules.temhandler.entity.TemData;

@Controller
@RequestMapping(value = "/Platform/TemData")
public class TemDataController {

    private static final Logger logger = LoggerFactory.getLogger(TemDataController.class);

    @Autowired
    private TemDataDubboClientService temDataClientService;

    /**
     * 
     * @Title: queryTemData 
     * @Description: 查询临时数据
     * @param request
     * @param businTableType
     * @param businTableId
     * @param businStatus   状态1刚插入  2已复核  3已审核   4 生效 9删除
     * @return 查询临时数据
     * @throws 
     * @author xuyp
     * @date 2017年12月26日 上午9:53:26
     */
    @RequestMapping(value = "/queryTemData", method = RequestMethod.POST)
    public @ResponseBody String queryTemData(HttpServletRequest request, String businTableType, String businTableId,
            String businStatus) {
        final Map<String, Object> anMap = Servlets.getParametersStartingWith(request, "");
        logger.info("入参：" + anMap);
        return ControllerExceptionHandler.exec(new ExceptionHandler() {
            @Override
            public String handle() {
                return AjaxObject
                        .newOk("查询临时数据成功", temDataClientService.queryTemData(businTableType, businTableId, businStatus))
                        .toJson();
            }
        }, "查询临时数据失败", logger);
    }

    /**
     * 
     * @Title: findTemDataByid 
     * @Description: 根据主键查询临时数据 
     * @param request
     * @param id
     * @return 根据主键查询临时数据 
     * @throws 
     * @author xuyp
     * @date 2017年12月26日 上午10:11:16
     */
    @RequestMapping(value = "/findTemDataByid", method = RequestMethod.POST)
    public @ResponseBody String findTemDataByid(HttpServletRequest request, Long id) {
        final Map<String, Object> anMap = Servlets.getParametersStartingWith(request, "");
        logger.info("入参：" + anMap);
        return ControllerExceptionHandler.exec(new ExceptionHandler() {
            @Override
            public String handle() {
                return AjaxObject.newOk("查询临时数据成功", temDataClientService.findTemDataByid(id)).toJson();
            }
        }, "查询临时数据失败", logger);
    }

    /**
     * 
     * @Title: saveAuditTemData 
     * @Description: 审核数据 
     * @param request
     * @param id
     * @param description
     * @return 审核数据
     * @throws 
     * @author xuyp
     * @date 2017年12月26日 上午9:57:16
     */
    @RequestMapping(value = "/saveAuditTemData", method = RequestMethod.POST)
    public @ResponseBody String saveAuditTemData(HttpServletRequest request, Long id, String description) {
        final Map<String, Object> anMap = Servlets.getParametersStartingWith(request, "");
        logger.info("入参：" + anMap);
        return ControllerExceptionHandler.exec(new ExceptionHandler() {
            @Override
            public String handle() {
                return AjaxObject.newOk("审核数据成功", temDataClientService.saveAuditTemData(id, description, anMap))
                        .toJson();
            }
        }, "审核数据失败", logger);
    }

    /**
     * 
     * @Title: saveDeleteTemData 
     * @Description: 删除数据
     * @param request
     * @param id
     * @param description
     * @return 删除数据
     * @throws 
     * @author xuyp
     * @date 2017年12月26日 上午10:08:15
     */
    @RequestMapping(value = "/saveDeleteTemData", method = RequestMethod.POST)
    public @ResponseBody String saveDeleteTemData(HttpServletRequest request, Long id, String description) {
        final Map<String, Object> anMap = Servlets.getParametersStartingWith(request, "");
        logger.info("入参：" + anMap);
        return ControllerExceptionHandler.exec(new ExceptionHandler() {
            @Override
            public String handle() {
                return AjaxObject.newOk("删除数据成功", temDataClientService.saveDeleteTemData(id, description)).toJson();
            }
        }, "删除数据失败", logger);
    }

    /**
     * 
     * @Title: saveAddTemData 
     * @Description: 编辑数据
     * @param request
     * @param temData
     * @return 编辑数据
     * @throws 
     * @author xuyp
     * @date 2017年12月26日 上午10:09:56
     */
    @RequestMapping(value = "/saveAddTemData", method = RequestMethod.POST)
    public @ResponseBody String saveAddTemData(HttpServletRequest request, TemData temData) {
        final Map<String, Object> anMap = Servlets.getParametersStartingWith(request, "");
        logger.info("入参：" + anMap);
        return ControllerExceptionHandler.exec(new ExceptionHandler() {
            @Override
            public String handle() {
                return AjaxObject.newOk("编辑数据成功", temDataClientService.saveAddTemData(temData)).toJson();
            }
        }, "编辑数据失败", logger);
    }

    /**
     * 
     * @Title: queryTemDataPage 
     * @Description: 分页查询 临时数据
     * @param businTableType  业务类全路径
     * @param businStatus 状态  多个状态用,隔开
     * @param anMap 前端传递的查询条件，参数可以传递业务中的参数（需要提前建立了映射）和temData的属性
     * @param flag 1 查询总条数
     * @param pageNum 当前页数
     * @param pageSize 每页显示的数量
     * @return TemData
     * @throws 
     * @author xuyp
     * @date 2017年12月28日 下午1:46:08
     */
    @RequestMapping(value = "/queryTemDataPage", method = RequestMethod.POST)
    public @ResponseBody String queryTemDataPage(HttpServletRequest request, String businTableType, String flag,
            String businStatus, int pageNum, int pageSize) {
        final Map<String, Object> anMap = Servlets.getParametersStartingWith(request, "");
        logger.info("入参：" + anMap);
        return ControllerExceptionHandler.exec(new ExceptionHandler() {
            @Override
            public String handle() {
                return AjaxObject.newOkWithPage("查询临时数据成功", temDataClientService.queryTemDataPage(businTableType,
                        businStatus, anMap, flag, pageNum, pageSize)).toJson();
            }
        }, "查询临时数据失败", logger);
    }

    /**
     * 
     * @Title: saveUpdateTemDataStatus 
     * @Description: 更新临时数据状态
     * @param request
     * @param ids
     * @param businStatus
     * @return 
     * @throws 
     * @author xuyp
     * @date 2018年1月5日 上午11:12:10
     */
    @RequestMapping(value = "/saveUpdateTemDataStatus", method = RequestMethod.POST)
    public @ResponseBody String saveUpdateTemDataStatus(HttpServletRequest request, String ids, String businStatus) {
        final Map<String, Object> anMap = Servlets.getParametersStartingWith(request, "");
        logger.info("入参：" + anMap);
        return ControllerExceptionHandler.exec(new ExceptionHandler() {
            @Override
            public String handle() {
                return AjaxObject.newOk("查询临时数据成功", temDataClientService.saveUpdateTemDataStatus(ids, businStatus))
                        .toJson();
            }
        }, "查询临时数据失败", logger);
    }

}
